package com.zzk.lanouhn.myapplication1.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.FindLunBoDetailsAdapter;
import com.zzk.lanouhn.myapplication1.modles.FindLunboDetailsModle;

import java.io.IOException;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/2.
 */
public class FindLnuBoDetails extends Activity {

    private RecyclerView recyclerView;
    private LinearLayout fanhui;

    private FindLunboDetailsModle detailsModle;
    private FindLunBoDetailsAdapter detailsAdapter;

    private ImageView iv_fenxiang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detailed_find_lnubo);
        recyclerView = (RecyclerView) findViewById(R.id.detailed_find_lnubo_recyclerView);
        fanhui = (LinearLayout) findViewById(R.id.detailed_find_lunbo_fanhui);
        iv_fenxiang = (ImageView) findViewById(R.id.detailed_find_lunbo_fengxiang);

        getDataLunBoDetaile();
        initData();
    }


    private void initData() {
        //返回
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //分享
        iv_fenxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showShare();
            }
        });
    }


    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }


    private void getDataLunBoDetaile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://chanyouji.com/api/articles/625.json?page=1";

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();

                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(FindLnuBoDetails.this, "失败1", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {

                        String result = response.body().string();
                        Gson gson = new Gson();

                        detailsModle = gson.fromJson(result, FindLunboDetailsModle.class);

                        if (detailsModle.getArticle_sections().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    detailsAdapter = new FindLunBoDetailsAdapter(FindLnuBoDetails.this, detailsModle);
                    recyclerView.setAdapter(detailsAdapter);

                    LinearLayoutManager llm = new LinearLayoutManager(FindLnuBoDetails.this);
                    recyclerView.setLayoutManager(llm);
                    break;
                case 2:
                    Toast.makeText(FindLnuBoDetails.this, "失败2", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };


}
